package com.yycx.msg.provider.service.impl;


import cn.hutool.core.map.MapUtil;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.yycx.msg.client.model.SmsMessage;
import com.yycx.msg.provider.configuration.SmsProperties;
import com.yycx.msg.provider.sms.SmsSender;
import lombok.extern.slf4j.Slf4j;

import com.tencentcloudapi.common.Credential;

import java.util.Map;

/**
 * 腾讯云短信实现类
 * @author zyf
 */
@Slf4j
public class TencentSmsSenderImpl implements SmsSender {


    private SmsProperties smsProperties;

    private final static String OK = "OK";

    private final static String CODE = "Code";

    public TencentSmsSenderImpl() {
        log.info("**********************************初始化腾讯云短信:" + this);
    }

    public TencentSmsSenderImpl(SmsProperties smsProperties) {
       this.smsProperties=smsProperties;
    }


    @Override
    public Boolean send(SmsMessage parameter) {
        boolean result = false;
        try {
            Credential cred = new Credential(smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret());
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("GET");
            httpProfile.setConnTimeout(10);
            httpProfile.setWriteTimeout(10);
            httpProfile.setReadTimeout(10);
            // 指定接入地域域名域域名访问
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            /* SDK默认用TC3-HMAC-SHA256进行签名
             * 非必要请不要修改这个字段 */
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setSmsSdkAppId(smsProperties.getAppId());
            req.setSignName(parameter.getSignName());
            req.setTemplateId(parameter.getTplCode());
            Map params= parameter.getTplParams();
            Integer code= MapUtil.getInt(params,"code");
            String[] array = new String[]{code+""};
            req.setTemplateParamSet(array);
            String[] phoneNumberSet = {"+86"+parameter.getPhoneNum()};
            req.setPhoneNumberSet(phoneNumberSet);
            SendSmsResponse res = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(res));
        } catch (Exception e) {
            log.error("发送短信失败：{}", e.getMessage(), e);
        }
        return result;
    }

    @Override
    public Boolean sendSmsCode(SmsMessage parameter) {
        parameter.setSignName(smsProperties.getSignName());
        parameter.setTplCode(smsProperties.getSmsTplCode());
        return send(parameter);
    }

    @Override
    public Boolean sendSms(SmsMessage parameter) {
        parameter.setSignName(smsProperties.getSignName());
        return send(parameter);
    }

    public SmsProperties getSmsProperties() {
        return smsProperties;
    }

    @Override
    public void setSmsProperties(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }
}
